/**
 * 跟单内容
 *
 * @author linfeng
 * @since 2019/10/23
 */
var form = null;

layui.define(['form', 'layedit', 'laydate'], function (exports) {
    var $ = layui.$;
    form = layui.form;
    var layer = layui.layer;
    var layedit = layui.layedit;
    var laydate = layui.laydate;

    form.render(null, "zmn-form");

    // 自定义验证规则
    form.verify({
        sort: function(value,item){
            if(value != null && value != ''){
                if (!/^[1-9]\d*$/.test(value)) {
                    return "排序值只能是正整数";
                }
                if(Number(value) > 100000){
                    return "排序最大100000";
                }
            }
        }
    });

    zmnLayUtil.layFormVerify(form);

    // 提交
    form.on('submit(zmn-form-submit)', function(obj){

        var documentaryDepartmentId = document.getElementsByName("documentaryDepartmentId");
        var departmentPriority = document.getElementsByClassName("departmentPriority");

        let array = new Array();
        for (var i = 0; i < 20; i++){
            var departmentVo = new Object();
            var documentaryDepartmentId = document.getElementsByName("documentaryDepartmentId" + i);

            if (documentaryDepartmentId == null){
                continue;
            }

            if (documentaryDepartmentId.length > 0){
                var index = documentaryDepartmentId[0].selectedIndex;        //拿到选中项(option)的索引(index)
                var value = documentaryDepartmentId[0][index].value;
                if (value != "" && value != 0 && value != "0"){
                    departmentVo['documentaryDepartmentId'] = value;
                    if (obj.field.id != null && obj.field.id != 0){
                        departmentVo['documentaryContentId'] = obj.field.id;
                    }

                    var departmentPriority = document.getElementsByName("departmentPriority" + i);
                    if (departmentPriority != null){
                        for (var j = 0; j < departmentPriority.length; j++){
                            if (departmentPriority[j].checked){
                                departmentVo['priority'] = (j + 1);
                                array.push(departmentVo);
                                break;
                            }
                        }
                    }
                }
            }
        }

        var engineerDocumentarySwitch = document.getElementById("engineerDocumentarySwitch");
        var serviceProviderDocumentarySwitch = document.getElementById("serviceProviderDocumentarySwitch");
        if (array.length == 0 && (!engineerDocumentarySwitch.checked)){
            layer.alert("跟单部门和工程师跟单至少勾选1项");
            return  false;
        }

        if (engineerDocumentarySwitch.checked){
            obj.field.engineerDocumentarySwitch = 2;
        } else {
            obj.field.engineerDocumentarySwitch = 1;
            obj.field.engineerDocumentaryPriority = null;
        }

        if (serviceProviderDocumentarySwitch.checked){
            obj.field.serviceProviderDocumentarySwitch = 2;
        } else {
            obj.field.serviceProviderDocumentarySwitch = 1;
            obj.field.serviceProviderDocumentaryPriority = null;
        }

        obj.field.departmentRelationsVOS = array;

        let bizTypeArr = new Array();
        let bizType = document.getElementsByName("bizType");
        for(let k in bizType){
            if(bizType[k].checked)
                bizTypeArr.push(bizType[k].value);
        }

        if(bizTypeArr.length == 0){
            layer.msg("业务类型不能为空");
            return false;
        }
        obj.field.bizType = bizTypeArr.join(",");

        var openId = layer.open({type: 3});
        var data = obj.field;
        var url = "/orderconfig/documentarycontent/addEdit.action";

        $.ajax({
            async: false,
            type: "POST", url: url,
            contentType: "application/json", dataType: "json", data: JSON.stringify(data),
            success:function(result) {
                layer.close(openId);
                if(result.status == 200) {
                    layer.msg(result.message);
                } else {
                    layer.msg(result.message, {"icon": 2});
                }
            },
            error:function(XMLHttpRequest, textStatus, errorThrown){
                layer.close(openId);
            }
        })
        return false;
    });

    //点击取消
    $(document).on('click', '#cancel', function () {
        let index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
        parent.layer.close(index);
    });

    form.on("select(documentaryDepartment)", function (obj) {
        var documentaryDepartments = document.getElementsByClassName('documentaryDepartmentClass');
        var size = documentaryDepartments.length;
        let array = new Array();
        for (var i = 0; i < size; i++){
            var selectedIndex = documentaryDepartments[i].selectedIndex;
            var selectedValue = documentaryDepartments[i][selectedIndex].value;
            array.push(selectedValue);
        }

        var count = 0;
        for (var i = 0; i < array.length; i++){
            if (array[i] == obj.value){
                count++;
            }
        }

        if (count >= 2){
            var opts = obj.elem;
            var length = opts.options.length;
            for (var i = 0; i < length; i++){
                if (i == 0){
                    obj.elem.options[i].selected = true;
                } else {
                    obj.elem.options[i].selected = false;
                }
            }

            form.render(null);
            layer.msg("跟单部门不能重复，请调整后再试");
        }
    });



    $(document).on('click', '#addDocumentaryDepartment', function () {

        var documentaryDepartments = document.getElementsByClassName('documentaryDepartmentClass');
        var size = documentaryDepartments.length;

        if (size >= 20){
            layer.alert('添加失败，责任部门最多20个');
            return false;
        }

        var options = "";
        for (var i = 0; i < json.departments.length; i++){
            options += '<option value="' + json.departments[i].deptId + '">' + json.departments[i].deptName + '</option>'
        }

        $("#documentaryDepartmentDiv").append('<div class="layui-input-block" id="documentaryDepartment' + size + '">\n' +
            '                            <div class="layui-input-inline">\n' +
            '                                <select class="documentaryDepartmentClass" lay-filter="documentaryDepartment" name="documentaryDepartmentId' + size + '" id="documentaryDepartmentId' + size + '" lay-search="">\n' +
            '                                    <option value="">请选择</option>\n' + options +
            '                                </select>\n' +
            '                            </div>\n' +
            '                            <i onclick="deleteCompoent(\'documentaryDepartment'+size+'\')" style="width: auto"  class="layui-input-inline layui-icon layui-icon-delete btn-del funcClass"></i>\n' +
            '                            <div class="layui-input-inline" style="width: 410px">\n' +
            '                                <label class="layui-form-label">优先级：</label>\n' +
            '                                <div class="layui-input-block">\n' +
            '                                    <input type="radio" class="departmentPriority" name="departmentPriority' + size + '" value="1" title="一级" checked="checked">\n' +
            '                                    <input type="radio" class="departmentPriority" name="departmentPriority' + size + '" value="2" title="二级" >\n' +
            '                                    <input type="radio" class="departmentPriority" name="departmentPriority' + size + '" value="3" title="三级">\n' +
            '                                </div>\n' +
            '                            </div>\n' +
            '                        </div>' +
            '<div class="layui-split" id="split' + size + '">&nbsp;</div>');

        form.render();
    });

    //对外暴露的接口
    exports('orderconfig_documentarycontent_edit', {});
});

function deleteCompoent(element) {
    var index = element.substring(21);
    var splitId = "split" + index;
    var compoent = document.getElementById(element);
    compoent.parentNode.removeChild(compoent);
    var splitCompoent = document.getElementById(splitId);
    splitCompoent.parentNode.removeChild(splitCompoent);
    form.render();
}